/***
 Copyright (c) 2013 CommonsWare, LLC

 Licensed under the Apache License, Version 2.0 (the "License"); you may
 not use this file except in compliance with the License. You may obtain
 a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

package com.example.lc.mydoubledisplay;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.view.Display;

/**
 * 多屏显示帮助类 lc 2015年
 */
public class PresentationHelper implements DisplayManager.DisplayListener {
    public interface Listener {
        void showGuangGao(Display display);

        void clearPreso(boolean switchToInline);
    }

    private Listener listener = null;
    private DisplayManager mgr = null;
    private Display current = null;
    private boolean isFirstRun = true;
    private boolean isEnabled = true;

    /**
     * 构造函数初始化一些数据
     *
     * @param ctxt     根据context获取到DisplayManager实例
     * @param listener 接口回调
     */
    public PresentationHelper(Context ctxt, Listener listener) {
        this.listener = listener;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            mgr = (DisplayManager) ctxt
                    .getSystemService(Context.DISPLAY_SERVICE);
        }
    }

    public void onResume() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            handleRoute();
            mgr.registerDisplayListener(this, null);
        }
    }

    public void onPause() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            listener.clearPreso(false);
            current = null;

            mgr.unregisterDisplayListener(this);
        }
    }

    /**
     * 激活
     */
    public void enable() {
        isEnabled = true;
        // handleRoute();
    }

    /**
     * 反激活
     */
    public void disable() {
        isEnabled = false;

        if (current != null) {
            listener.clearPreso(true);
            current = null;
        }
    }

    /**
     * 获取当前激活 状态
     *
     * @return boolean
     */
    public boolean isEnabled() {
        return (isEnabled);
    }

    /**
     * 处理不同显示操作类型
     */
    private void handleRoute() {
        if (isEnabled()) {
            Display[] displays = mgr
                    .getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);

            if (displays.length == 0) {
                if (current != null || isFirstRun) {
                    listener.clearPreso(true);
                    current = null;
                }
            } else {
                Display display = displays[0];
                // boolean isValid() Returns true if this display is still
                // valid, false if the display has been removed.
                if (display != null && display.isValid()) {
                    if (current == null) {
                        // 接口回调--用户屏第一次显示的内容
                        listener.showGuangGao(display);
                        // 当前显示内容
                        current = display;
                    } else if (current.getDisplayId() != display.getDisplayId()) {
                        listener.clearPreso(true);
                        listener.showGuangGao(display);
                        current = display;
                    } else {
                        System.out.println("同一个内容屏幕");
                    }
                } else if (current != null) {
                    listener.clearPreso(true);
                    current = null;
                }
            }

            isFirstRun = false;
        }
    }

    /**
     * 增加显示
     */
    public void onDisplayAdded(int displayId) {
        handleRoute();
    }

    /**
     * 改变显示
     */
    public void onDisplayChanged(int displayId) {
        handleRoute();
    }

    /**
     * 清除显示
     */
    public void onDisplayRemoved(int displayId) {
        handleRoute();
    }
}